import numpy as np
import pandas as pd

# 创建字典型series结构
data = {'Name': pd.Series(['小明', '小亮', '小红', '小华']),
        'Age': pd.Series([25, 34, 24]),
        'Rating': pd.Series([5.00, 4.0, 3.00])}

df = pd.DataFrame(data)

print(df)
print(f'sum:{df.filter(items=[2]).sum()}')
print(f'sum:{df.sum(axis=1)}')  # 每列相加
print(f'mean:{df.mean()}')
print(f'median:{df.median()}')
print(f'std:{df.std()}')
print(f'min:{df.min()}')
print(f'max:{df.max()}')
print(f'describe:{df.describe()}')

# 筛选过滤
print(df['Age'] <= 30)
print(df[df['Age'] <= 30])
print(df.loc[df['Age'] <= 30])
print(df[(df['Age'] <= 30) & (df['Rating'] <= 4)])

df2 = pd.DataFrame([[1, 2, 3], [5, 6, 7], [1, 1, 1], [2, 2, 2]], columns=['c1', 'c2', 'c3'])
print(df2, "-----df2------>")


# 自定义函数
def adder(ele1, ele2):
    return ele1 + ele2


# pipe操作整个df
print(df2.pipe(adder, 3), "-----自定义------>")

# apply操作行或列
print(df2.apply(np.mean), "------mean----->")
print(df2.apply(lambda x: x.max() - x.min()), "----差值------->")

print(df2['c1'].map(lambda x: x >= 0))
print(df2['c1'].map(lambda x: x * 100))

# 操作单一元素
print(df2.applymap(lambda x: x * 10))  # 每个元素
